﻿using Easy.Common.Config;
using Easy.Services.Entities;
using Microsoft.EntityFrameworkCore;

namespace Easy.Services.DbContexts
{
    /// <summary>
    /// MySQLDbContext
    /// </summary>
    public partial class MySQLDbContext : DbContext
    {

        //private ILoggerFactory loggerFactory = new LoggerFactory().CreateLogger(l=>l.AddConsole());
        //private ILoggerFactory loggerFactory = new LoggerFactory().CreateLogger<MySQLDbContext>();


        #region OnConfiguring
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //base.OnConfiguring(optionsBuilder);
            var mySqlEnable = AppSettings.GetEnable("dbConn", "dbMySql");
            if (mySqlEnable)
            {
                string mySqlConnString = AppSettings.GetConnection("dbConn", "dbMySql", "EasyMySql");
                optionsBuilder.UseMySQL(mySqlConnString);
            }
            //optionsBuilder.UseLoggerFactory(loggerFactory);
        }
        #endregion

        #region OnModelCreating

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
        }

        #endregion

        #region Model

        public DbSet<Company> Companies { get; set; }

        public DbSet<Permission> Permissions { get; set; }

        public DbSet<Role> Roles { get; set; }

        public DbSet<RolePermissionRelation> RolePermissionRelations { get; set; }

        public DbSet<User> Users { get; set; }

        public DbSet<UserRoleRelation> UserRoleRelations { get; set; }


        #endregion
    }
}
